package cn.sunzq.springdata.elasticsearch.demo.config;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;

/**
 * @创建人 孙中泉
 * @创建时间 2021/7/19
 * @描述
 */
@Configuration
public class RestClientByPasswordConfig extends AbstractElasticsearchConfiguration {

    @Value("${es.connect-timeout:-1}")
    private int connectTimeout;

    @Value("${es.socket-timeout:-1}")
    private int socketTimeout;

    @Value("${es.connection-request-timeout:-1}")
    private int connectionRequestTimeout;

    @Value("${es.username:es_user}")
    private String userName;

    @Value("${es.password:FXP7YnnKrZoprqET}")
    private String password;

    @Value("${es.hosts:10.122.149.201}")
    private String hosts;

    @Value("${es.port:9200}")
    private int port;

    @Value("${es.max-retry-timeout-millis:300000}")
    private int maxRetryTimeoutMillis;

    @Override
    public RestHighLevelClient elasticsearchClient() {
        String[] hosts = this.hosts.split(",");
        HttpHost[] httpHosts = new HttpHost[hosts.length];
        for(int i=0;i<hosts.length;i++) {
            httpHosts[i] = new HttpHost(hosts[i], port, "http");
        }

        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));

        RestClientBuilder builder = RestClient.builder(httpHosts).setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
            @Override
            public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
                requestConfigBuilder.setConnectTimeout(connectTimeout);
                requestConfigBuilder.setSocketTimeout(socketTimeout);
                requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeout);
                return requestConfigBuilder;
            }
        }).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
            @Override
            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                httpClientBuilder.disableAuthCaching();
                return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
            }
        });

        RestHighLevelClient client = new RestHighLevelClient(builder);
        return client;
    }

    @Bean(name = "restHighLevelClient")
    public RestHighLevelClient client() {
        return this.elasticsearchClient();
    }
}
